<?php


namespace app\admin\controller;


use app\admin\business\adminBus;
use think\facade\View;

class Admin extends AdminBase
{
    public function index(){
        return View::fetch();
    }
    public function getAdminList(){
        $adminBus = new adminBus();
        try {
            $result = $adminBus->getAdminListBus();
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$result);
    }
    public function edit(){
        $id = $this->request->param('id');
        View::assign('id',$id);
        return View::fetch();
    }
    public function getAdminById(){
        $id = $this->request->param('id');
        $adminBus = new adminBus();
        try {
            $result = $adminBus->getAdminByIdBus($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$result);
    }
    public function getRuleList(){
        $adminBus = new adminBus();
        try {
            $result = $adminBus->getRuleListBus();
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$result);
    }
    public function updateAdminByWhere(){
        $id = $this->request->param('id');
        $name = $this->request->param('name');
        $roleName = $this->request->param('roleName');
        $rule = $this->request->param('rule');
        if($id == 1){
            return show(config('status.error'),'超级管理员无法修改');
        }
        $validateData = [
            'id' => $id,
            'name_two' => $name,
            'roleName' => $roleName,
            'rule' => $rule,
        ];
//        dump($validateData);exit();
        $validate = new \app\admin\validate\AdminUser();
        if(!$validate->scene('admin_edit')->check($validateData)){
            return show(config('status.error'),$validate->getError());
        }
        $where = [
            'id' => $id,
        ];
        $data = [
            'name_two' => $name,
            'roleName' => $roleName,
            'rule' => $rule,
        ];
        $adminBus = new adminBus();
        try {
            $result = $adminBus->updateAdminByWhereBus($where,$data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'修改成功',$result);
    }
    public function add(){
        return View::fetch();
    }
    public function addAdmin(){
        $name = $this->request->param('name');
        $password = $this->request->param('password');
        $roleName = $this->request->param('roleName');
        $rule = $this->request->param('rule');
        $validateData = [
            'name' => $name,
            'password' => $password,
            'roleName' => $roleName,
            'rule' => $rule,
        ];
        $validate = new \app\admin\validate\AdminUser();
        if(!$validate->scene('admin_add')->check($validateData)){
            return show(config('status.error'),$validate->getError());
        }
        $adminBus = new adminBus();
        try {
            $result = $adminBus->addAdminBus($validateData);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'添加成功',$result);
    }
    public function resetPassword(){
        $id = $this->request->param('id');
        View::assign('id',$id);
        return View::fetch();
    }
    public function setPasswordByWhere(){
        $id = $this->request->param('id');
        $password = $this->request->param('password');
        $validateData = [
            'id'=>$id,
            'password' => $password
        ];
        $validate = new \app\admin\validate\AdminUser();
        if(!$validate->scene('admin_password')->check($validateData)){
            return show(config('status.error'),$validate->getError());
        }
        $adminBus = new adminBus();
        try {
            $result = $adminBus->setPasswordByWhereBus($id,$password);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'设置成功',$result);
    }
    public function removeByWhere(){
        $id = $this->request->param('id');
        $where = [
            'id'=>$id
        ];
        $adminBus = new adminBus();
        try {
            $res = $adminBus->removeByWhereBus($where);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'删除成功',$res);
    }
}